import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:hc_bot_kit/service/service.dart';
import 'widgets/poi_list.dart';

class GuideSettingController extends GetxController {
  GuideSettingController();

  String get robotSDKIPAddress => Get.find<RobotHardwareService>().ipAddress;

  String get originPoi => Get.find<RobotHardwareService>().originPoi;

  _initData() {
    update(["guidesettin"]);
  }

  void onTap() {}

  // @override
  // void onInit() {
  //   super.onInit();
  // }

  @override
  void onReady() {
    super.onReady();
    _initData();
  }

  // @override
  // void onClose() {
  //   super.onClose();
  // }

  void onTapIP() async {
    await changeHardwareIP();
    update(["guidesettin"]);
  }

  void onTapPoi() async {
    await changePoi();
    update(["guidesettin"]);
  }
}

extension on GuideSettingController {
  Future<String> pickPoi() async {
    await Get.find<RobotHardwareService>().connect();
    await Future.delayed(2.seconds);
    final poiList = await Get.find<RobotHardwareService>().getAllPoi();
    debugPrint(poiList.toString());
    final poi = await Get.to(PoiListPage(poiList: poiList));
    return poi ?? "";
  }

  Future changePoi() async {
    final poi = await pickPoi();
    await Get.find<RobotHardwareService>().saveOriginPoi(poi);
  }

  Future changeHardwareIP() async {
    var ip = "";
    await Get.defaultDialog(
      middleText: "Input ip address",
      onConfirm: () async {
        if (ip.isEmpty) {
          return;
        }
        Get.back();
      },
      onCancel: () {
        ip = "";
      },
      content: TextField(
        autofocus: true,
        onChanged: (value) {
          ip = value;
        },
      ),
    );
    if (ip.isEmpty) {
      return;
    }
    await Get.find<RobotHardwareService>().saveIP(ip);
  }
}
